package by.grsu.hdm.servlets;

import java.io.IOException;

import by.grsu.hdm.exeption.SongNotFoundException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.PrintWriter;

import by.grsu.hdm.devices.MediaCenter;
import by.grsu.hdm.model.Audio;
import by.grsu.hdm.model.Video;

public class MediaPlayerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public MediaPlayerServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = response.getWriter();
		pw.print("<head>");
		pw.print("<script type=\"text/javascript\">");
		pw.print("function viewStatus(f) {");

		pw.print("if(f.group1[0].checked) {alert(\"play\");} ");
		pw.print("if(f.group1[1].checked) {alert(\"stop\");} ");
		pw.print("if(f.group1[2].checked) {alert(\"pause\");} }");

		pw.print("</script>");
		pw.print("</head>");

		MediaCenter media = new MediaCenter("Media#256");

		String s;
		s = request.getParameter("inputText"); // output audio composition
			
		if (s == "") {
			int i = 0;
			pw.print("<h3>Музыка:</h3>");
			for (Audio comp : media.contactMediaPlayer().audioList) {
				++i;
				pw.print(String.valueOf(i) + "<b>.Имя:</b>:");
				pw.print(comp.name + "<br>");
				pw.print("&#160&#160&#160"); // 3*x space
				pw.print("<b>Размер:</b>");
				pw.print(String.valueOf(comp.size) + "<br>");

			}
			i = 0;
			pw.print("<h3>Фильмы:</h3>");
			for (Video comp : media.contactMediaPlayer().videoList) {
				++i;
				pw.print(String.valueOf(i) + "<b>.Имя:</b>");
				pw.println(comp.name + "<br>");
				pw.print("&#160&#160&#160");
				pw.print("<b>Размер:</b>");
				pw.println(String.valueOf(comp.size) + "<br>");

			}

		}
		// search
		int k = 0;
		if (s != "") {

			for (Audio comp : media.contactMediaPlayer().audioList) {
				if (comp.name.equals(s)) {
					++k;
				}
			}
			for (Video comp : media.contactMediaPlayer().videoList) {
				if (comp.name.equals(s)) {
					++k;
				}
			}
			if (k == 0) {
				try {
					throw new SongNotFoundException();
				} catch (SongNotFoundException e) {
					pw.print("Композиция не найдена(((");
					return;
				}
				// throw new Exception
			}
			// out
			pw.print("Найдено " + String.valueOf(k) + " композиций<br>");
			for (Audio comp : media.contactMediaPlayer().audioList) {
				if (comp.name.equals(s)) {
					pw.print("&#160&#160&#160");
					pw.print("<b>Имя:</b>:");
					pw.print(comp.name + "<br>");
					pw.print("&#160&#160&#160"); // 3*x space
					pw.print("<b>Размер:</b>");
					pw.print(String.valueOf(comp.size) + "<br>");
				}
			}
			for (Video comp : media.contactMediaPlayer().videoList) {
				if (comp.name.equals(s)) {
					pw.print("&#160&#160&#160");
					pw.print("<b>Имя:</b>:");
					pw.print(comp.name + "<br>");
					pw.print("&#160&#160&#160"); // 3*x space
					pw.print("<b>Размер:</b>");
					pw.print(String.valueOf(comp.size) + "<br>");
				}
			}
			s = request.getParameter("inputText");
			pw.print("<form action=\"helperMPS\" method=\"POST\">");

			// onClick=\"viewStatus(this.form)\"
			pw.print("<input type=\"radio\" name=\"group1\" value=\"Play\" > Play<br> ");
			pw.print("<input type=\"radio\" name=\"group1\" value=\"Stop\" > Stop<br> ");
			pw.print("<input type=\"radio\" name=\"group1\" value=\"Pause\" > Pause<br> ");
			pw.print("<input type=\"submit\" value\"Принять\"> ");
			pw.print("<input type=\"radio\" name=\"group2\" value=\"" + s+ "\" style=\"display:none\" checked ><br> ");
			pw.print("</form>");

		}

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
